﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CI.Caja.BL;
using CI.Caja.BE;

namespace CI.Caja.MVC.Controllers
{
    public class TurnoController : Controller
    {
        //
        // GET: /Turno/
        //Dependencias
        public IBL_Turno TurnoService { get; set; }

        public ActionResult Index()
        {
            ICollection<BE_Turno> modelo = TurnoService.ListarTurno();
            return View(modelo);
        }

        //
        // GET: /Turno/Details/5

        public ActionResult Details(string id)
        {
            BE_Turno modelo = TurnoService.ObtenerTurno(id);

            return View(modelo);
        }

        //
        // GET: /Turno/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Turno/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    string idturno = TurnoService.ObtenerCodigo();
                    string descripTurno = (string)collection["DescripTurno"];
                    string horaInicio = (string)collection["HoraInicio"];
                    string horaFin = (string)collection["HoraFin"];

                    if (TurnoService.ValidaDuplicado(descripTurno, horaInicio, horaFin) != 0) {
                        throw new Exception("No puede ingresar un turno duplicado");
                    }

                    TurnoService.RegistrarTurno(idturno, descripTurno, horaInicio, horaFin);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex.Message);
                return View();
            }
        }
        
        //
        // GET: /Turno/Edit/5
 
        public ActionResult Edit(string id)
        {
            BE_Turno modelo = TurnoService.ObtenerTurno(id);
            return View(modelo);
        }

        //
        // POST: /Turno/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, FormCollection collection)
        {
            try
            {
                string descripTurno = (string)collection["DescripTurno"];
                string horaInicio = (string)collection["HoraInicio"];
                string horaFin = (string)collection["HoraFin"];
                TurnoService.ActualizarTurno(id, descripTurno, horaInicio, horaFin);
                return RedirectToAction("Index");


            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex.Message);
                BE_Turno modelo = TurnoService.ObtenerTurno(id);
                return View(modelo);
            }
        }

        //
        // GET: /Turno/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Turno/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
